﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet.Entity.Common.Web;
using BaseSet;
using System.Data;

namespace WebSite
{
    public partial class adc : System.Web.UI.Page
    {
        //广告统计页面
        protected void Page_Load(object sender, EventArgs e)
        {
            string adid = Request.QueryString["id"] + "";//广告ID
            string aduserid = Request.QueryString["userid"] + "";//呈现广告页面的用户ID
            string linkurl = Request.QueryString["linkurl"] + "";//广告的链接地址
            string isuserad = Request.QueryString["isuserad"] + "";//传递过来的是否为该用户的广告 0否1是
            string isuseradposition = Request.QueryString["isuseradposition"] + "";//传递过来的是否为该用户的广告位 0否1是

            Users uinfo = UserBase.GetCurrentUser();
            string ipaddress = IP.GetIPAddress();
            //判断是否已经点击过该广告
            AdCountHelper achelper = new AdCountHelper();
            Criteria c = new Criteria();

            if (uinfo == null)
            {
                c.Add(CriteriaType.Equals, "IP", ipaddress);
            }
            else
            {
                c.Add(CriteriaType.Equals, "UsersID", uinfo.ID);
            }

            int count = achelper.QueryAdCountCountBySearch(c);
            if (count > 0)//已经点击过广告则不进行统计
            {
                Response.Redirect(linkurl);
            }
            else
            {
                //增加该广告点击记录
                AdCount adcinfo = new AdCount();
                adcinfo.AdID = adid;
                adcinfo.IP = ipaddress;
                if (uinfo != null)
                {
                    adcinfo.UsersID = uinfo.ID;
                }

                achelper.AddAdCount(adcinfo);

                //增加该广告的点击次数
                Ad ainfo = new Ad();
                AdHelper ahelper=new AdHelper();
                ainfo = ahelper.GetAd(adid);
                int clickcount = ainfo.ClickCount;
                clickcount++;
                ainfo.ClickCount = clickcount;
                ahelper.EditAd(ainfo);

                //如果传递过来的用户是系统广告人员则直接跳转
                string systemaduserid = System.Configuration.ConfigurationSettings.AppSettings["systemaduserid"] + "";
                if (aduserid == systemaduserid)
                {
                    Response.Redirect(linkurl);
                }
                else
                {
                    //判断该广告位是否为该用户的
                    if (isuseradposition == "1")//广告位是用户的(用户已经购买)
                    {
                        //判断该广告是否为该用户的
                        if (isuserad == "1")//广告是用户的
                        {
                            //目前不对该用户进行收费
                            Response.Redirect(linkurl);
                        }
                        else//广告不是该用户的 占用了该用户的广告位 需要为该用户分配金额
                        {
                            Users aduinfo = new Users();
                            UsersHelper uhelper = new UsersHelper();
                            aduinfo = uhelper.GetUsers(aduserid);
                            string adroleid = aduinfo.RoleID;
                            //查找该用户级别下单个广告点击分到的金额
                            Role rinfo = new Role();
                            RoleHelper rhelper = new RoleHelper();
                            rinfo = rhelper.GetRole(adroleid);
                            string advertratio = rinfo.AdvertRatio;
                            if (!string.IsNullOrEmpty(advertratio))
                            {
                                float adratio = (float)Convert.ToDouble(advertratio);
                                if (adratio > 0)//单次点击广告费用不为0
                                {
                                    if (aduserid != "")
                                    {
                                        CashFlow cfinfo = new CashFlow();
                                        CashFlowHelper cfhelper = new CashFlowHelper();
                                        cfinfo.UsersID = aduserid;
                                        cfinfo.Money = adratio;
                                        cfinfo.Type = 3;
                                        cfinfo.Description = "广告位" + ainfo.AdPositionName + "获得广告分成";
                                        cfhelper.AddCashFlow(cfinfo);
                                    }
                                }
                            }
                            Response.Redirect(linkurl);
                        }
                    }
                    else//广告位不是用户的
                    {
                        Response.Redirect(linkurl);
                    }
                }
                
            }
        }
    }
}